package org.jeecg.modules.platform.organization.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.api.entity.SysPermission;
import org.jeecg.modules.platform.entity.CyPlatformOrganizationPermission;
import org.jeecg.modules.platform.organization.mapper.CyPlatformOrganizationPermissionMapper;
import org.jeecg.modules.platform.organization.service.ICyPlatformOrganizationPermissionService;
import org.jeecg.modules.system.service.ISysPermissionService;
import org.jeecg.modules.system.service.ISysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 组织权限
 * @Author: jeecg-boot
 * @Date: 2024-10-28
 * @Version: V1.0
 */
@Service
public class CyPlatformOrganizationPermissionServiceImpl extends ServiceImpl<CyPlatformOrganizationPermissionMapper, CyPlatformOrganizationPermission> implements ICyPlatformOrganizationPermissionService {

    @Autowired
    @Lazy
    private ISysPermissionService permissionService;
    @Autowired
    @Lazy
    private ISysRolePermissionService rolePermissionService;

    @Override
    public void saveBatch(String organizationId, List<CyPlatformOrganizationPermission> selectModules) {
        List<CyPlatformOrganizationPermission> permissionList = selectModules.stream()
                .map(module -> new CyPlatformOrganizationPermission().setOrganizationId(organizationId).setModuleId(module.getModuleId()).setExpire(module.getExpire())).collect(Collectors.toList());
        this.saveBatch(permissionList);
    }

    @Override
    public void update(String organizationId,String approveUser, List<CyPlatformOrganizationPermission> selectModules) {
        this.remove(new LambdaQueryWrapper<CyPlatformOrganizationPermission>().eq(CyPlatformOrganizationPermission::getOrganizationId, organizationId));
        if (CollUtil.isNotEmpty(selectModules)) {
            saveBatch(organizationId, selectModules);
            rolePermissionService.updateRolePermission(organizationId,approveUser);
        }
    }

    @Override
    public List<String> listModulesByOrg(String orgId) {
        List<CyPlatformOrganizationPermission> list = this.list(new LambdaQueryWrapper<CyPlatformOrganizationPermission>()
                .gt(CyPlatformOrganizationPermission::getExpire, new Date())
                .eq(CyPlatformOrganizationPermission::getOrganizationId, orgId));
        return Optional.ofNullable(list)
                .map(permissionList -> permissionList.stream().map(CyPlatformOrganizationPermission::getModuleId).collect(Collectors.toList()))
                .orElse(Collections.emptyList());
    }

    @Override
    public List<SysPermission> listOrgPermission(String orgId) {
        List<String> moduleList = this.listModulesByOrg(orgId);
        return permissionService.listPermissionAndChild(moduleList);
    }

    @Override
    public void registerPermission(String orgId) {
        List<SysPermission> sysPermissions = this.permissionService.listModulesTree(false);
        List<CyPlatformOrganizationPermission> permissionList = sysPermissions.stream()
                .filter(sysPermission -> !sysPermission.getUrl().matches(".*module_admin.*"))
                .map(sysPermission ->
                        new CyPlatformOrganizationPermission().setOrganizationId(orgId)
                                .setModuleId(sysPermission.getId())
                                .setExpire(DateUtil.offsetDay(new Date(), 7))
                ).collect(Collectors.toList());
        this.saveBatch(permissionList);
    }

}
